/**
 * <p>SystemName: WLBSNS</p>
 * <p>Description: 微乐帮社交网络 </p> 
 * <P>FileName: EncodingFilter.java</P>
 * <p>Copyright: Copyright (c) 2013 </p> 
 * <p>Company: </p> 
 * @author Theron 
 * @date 2013年11月10日 下午4:53:14 
 * @version V1.0 
 */
package com.wlbsns.framework.web;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import com.wlbsns.framework.consts.WLBConsts;
import com.wlbsns.framework.tools.StrUtils;

/**
 * 
 * <p>FileName: EncodingFilter.java </p>
 * <p>PackageName: com.wlbsns.framework.web </p>
 * <p>ClassName: EncodingFilter </p>
 * <p>Desciption: 系统字符编码过滤器 </p>
 * @author Theron
 * @date 2013年11月10日 下午5:32:05
 */
public class EncodingFilter implements Filter {

	FilterConfig config; // 过滤器配置对象
	private String encoding = "UTF-8"; // 定义私有变量存放编码

	/**
	 * Default constructor.
	 */
	public EncodingFilter() {
	}

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {

		if (config != null) {
			HttpServletRequest httpRequest = (HttpServletRequest) request;

			httpRequest.setCharacterEncoding(StrUtils.isEmpty(config
					.getInitParameter(WLBConsts.ENCODING)) ? encoding : config
					.getInitParameter(WLBConsts.ENCODING));
		}

		// pass the request along the filter chain
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		this.config = fConfig;
	}

}
